<template>
    <el-dialog
            title="修改库存"
            :visible.sync="dialogVisible"
            width="50%"
            :close="close"
            :before-close="close">
        <el-form ref="form" :model="goodsItem" label-width="80px">
            <el-form-item label="商品编码">
                <span> {{goodsItem.goodsCode}} </span>
            </el-form-item>
            <el-form-item label="商品名称">
                <span> {{goodsItem.goodsName}} </span>
            </el-form-item>
            <el-form-item label="库存">
                <div v-for="(item, i) in stockList" :key="i">
                    <div v-if="!addStock">
                        <el-input-number size="small" v-model="item.count" :min="0"></el-input-number> <el-tag type="success">{{ item.unit }}</el-tag>
                    </div>
                    <div v-else>
                        (进货)
                        <el-input-number size="small" v-model="item.count" :min="0"></el-input-number> <el-tag type="success">{{ item.unit }}</el-tag>
                    </div>
                </div>
            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="!addStock" @click="updateStock">提 交</el-button>
            <el-button type="primary" v-else @click="doAddStock">进 货</el-button>
            <el-button @click="close">取 消</el-button>
        </span>
    </el-dialog>
</template>

<script>
import API from '@/api/api';
export default {
    name: "StockEdit",
    props: ["goods", "addFlag"],
    data() {
        return {
            // 对话框标志位
            dialogVisible: true,
            // 进货标志位
            addStock: false,
            // 库存列表
            stockList: [],
            // 参数
            goodsItem: {
                goodsCode: '',
                goodsName: '',
                stockList: [],
            },
        }
    },
    mounted() {
        this.addStock = this.addFlag;
        if (this.goods) {
            this.goodsItem = JSON.parse(JSON.stringify(this.goods));
        }
        this.getStockList();
    },
    methods: {
        close() {
            this.$emit("close");
        },
        // 查库存
        getStockList() {
            let url = API.stock.selectUnitStock;
            let param = this.goods;
            this.$get(url, param).then(res => {
                let data = res.data;
                if (data.success) {
                    this.stockList = data.data;
                }
                if (this.addStock) {
                    this.stockList.forEach(item => item.count = 0);
                }
            }).catch( err => {
                this.$message.error("请求失败!");
            })
        },
        // 更新库存
        updateStock() {

            let url = API.goods.update;
            let temp = JSON.parse(JSON.stringify(this.stockList));
            temp = temp.filter(item => item.count > 0);
            this.goodsItem.stockList = temp;
            let param = JSON.parse(JSON.stringify(this.goodsItem));
            param.originGoodsCode = param.goodsCode;
            param.flag = null;
            param.status = param.status === '可用'? '1':'0';
            this.$post(url, param).then( res => {
                let data = res.data;
                if (data.success) {
                    this.$message.closeAll();
                    this.$message({
                        type: 'success',
                        message: "修改成功!",
                        showClose: true,
                    });
                    // this.close();
                } else {
                    this.$message.closeAll();
                    this.$message.error(data.msg);
                }
            }).catch( err => {
                this.$message.error("请求失败!");
            })
        },
        // 进货
        doAddStock() {
            console.log("进货~~~~~~~~~~~~~~~");
            // 校验
            let cnt = 0;
            this.stockList.forEach(item => cnt += item.count);
            if (cnt === 0) {
                this.$message.warning("进货数据不能为‘0’!");
                return;
            }

            let url = API.stock.updateStock;
            let param = {};
            param.goodsCode = this.goodsItem.goodsCode;
            param.stockList = JSON.parse(JSON.stringify(this.stockList)).filter(item => item.count > 0);
            param.inOrOut = "IN";
            this.$post(url, param).then(res => {
                let data = res.data;
                if (data.success) {
                    this.$message.success({
                        showClose: true,
                        message: "进货成功!",
                    });
                    // this.close();
                }
            }).catch( err => {
                this.$message.closeAll();
                this.$message.error("请求失败!");
            })
        }
    }
}
</script>

<style lang="scss" scoped>
    /deep/.el-input__inner {
        padding: 0 8px;
    }
</style>
